//
//  PurchaseCarController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/15.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
//新车代购
class PurchaseCarController:BaseController{
    var purchaseCarView: PurchaseCarView = {
        let view = PurchaseCarView.init(frame: CGRect(x: 0, y: kStatusBarH + kNavigationBarH, width: kScreenW, height:kScreenH - kStatusBarH - kNavigationBarH))
        return view
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupNavBar(title: "新车代购", isback: true, appearWay: 2, isright: false)//自定义导航
        self.view.addSubview(purchaseCarView)
    }
}
extension PurchaseCarController{
    //提交按钮
    func submitPurchClick() {
        if "" ==  self.purchaseCarView.carType.text! {
            checkAlertFun(message: "请输入车辆类型！")
        }
        else if "" ==  self.purchaseCarView.price.text! {
            checkAlertFun(message: "请输入价格范围！")
        }
        else if "" ==  self.purchaseCarView.buyerName.text! {
            checkAlertFun(message: "请输入车主姓名！")
        }
        else if "" ==  self.purchaseCarView.buyerPhone.text! {
            checkAlertFun(message: "请输入车主联系电话！")
        }else {
            submitPurchFunc()
        }
    }
    //提交信息
    func submitPurchFunc(){
        if reachability.isReachable {
            let urlString = "\(projectUrl)/order/other/commit/carpurchase/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
            let parameter:[String:Any] = [
                "orderData":"{\"carType\":\"\(self.purchaseCarView.carType.text!)\",\"price\":\"\(self.purchaseCarView.price.text!)\",\"buyerName\":\"\(self.purchaseCarView.buyerName.text!)\",\"buyerPhone\":\"\(self.purchaseCarView.buyerPhone.text!)\",\"remark\":\"\(self.purchaseCarView.remark.text!)\"}"
            ]
            Alamofire.request(urlString, method: .post, parameters: parameter, headers : headers).responseJSON { (response) in
                if response.result.isSuccess{
                    let json = JSON(response.result.value as! [String : AnyObject])
                    if json["success"] == true {
                        let alertController = UIAlertController(title: alertTitle, message: "新车代购信息提交成功！", preferredStyle: .alert)
                        let okAction = UIAlertAction(title: "确定", style: .default, handler:{
                            (UIAlertAction) -> Void in
                            let mainVC = UINavigationController(rootViewController: MainViewController())
                            app.window?.rootViewController = mainVC
                        })
                        alertController.addAction(okAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                    else{
                        self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
                    }
                }
                else{
                    print(response.result.error!) //输出错误日志
                }
            }
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}

